package com.liangzhong.model.guide
{
	import com.common.utils.Reflection;
	import com.common.utils.TimeFrameUtil;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.constants.TaskConst;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.liangzhong.model.guide.vo.GuideClubVo;
	import com.liangzhong.model.guide.vo.GuideGiftSelectVo;
	import com.liangzhong.model.guide.vo.GuidePlayerSelectVo;
	import com.liangzhong.model.guide.vo.GuidePlayerVo;
	import com.liangzhong.model.guide.vo.GuideSelfSelectVo;
	import com.liangzhong.model.guide.vo.GuideSettingVo;
	import com.liangzhong.model.guide.vo.GuideVo;
	import com.liangzhong.model.task.vo.TaskAwardVo;
	import com.liangzhong.model.task.vo.TaskInfoVo;
	import com.lz.football.module.guide.Guide;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;

	/**
	 * Guide Manager
	 * @author lake
	 * 
	 */	
	public class GuideManager extends EventDispatcher
	{
		/**
		 * 引导配置信息
		 */		
		public var guideConfigItems 		:Array;
		protected var _acceptedList		:Array;
		public var guideDatasDic			:Dictionary;
		public var guideViewDic				:Dictionary;
		public var curGuide					:Guide;
		/**
		 *选择的角色  ---- 前锋，中场，后卫  
		 */		
		public var selectPlayers			:Array;		
		/**
		 *选择球队的id 
		 */		
		public var selectTeamId :int = 1;
		/**
		 *选择的球星 
		 */		
		public var selectStarVo :PlayerVo;
		/**
		 *巨星加盟步骤索引 
		 */		
		public var starJoinGuideIndex :int = 1;
		/**
		 *是否配置完毕 
		 */		
		public var isConfigFinish :Boolean = false;
		///////////////////////////////华丽分割线/////////////////////////////////////////////
		/**
		 *是否重新设定 
		 */		
		public var isReset		:Boolean = false;
		/**
		 *俱乐部信息 
		 */		
		public var guideClubVo :GuideClubVo;
		/**
		 * 核心球员选择
		 */		
		public var guidePlayerSelectVo :GuidePlayerSelectVo;
		/**
		 *自己 
		 */		
		public var guideSelfSelectVo :GuideSelfSelectVo;
		/**
		 *礼包 
		 */		
		public var guideGiftSelectVo :GuideGiftSelectVo;
		/**
		 *引导容器 
		 */		
		public var guideContainer	  :Sprite;
		/**
		 *引导场景容器 添加到主界面 
		 */		
		public var sceneContainer	  :Sprite;
		/**
		 *新手设置vo 
		 */		
		public var guideSettingVo	  :GuideSettingVo;
		
		public function GuideManager()
		{
			//旧引导
			/*guideConfigItems = [];
			_acceptedList = [];
			selectPlayers = [];
			guideDatasDic = new Dictionary();
			guideViewDic = new Dictionary();
			TimeFrameUtil.instance.addMethod(onEnterFrame);*/
			//新引导
			/*guideClubVo = new GuideClubVo();
			guidePlayerSelectVo = new GuidePlayerSelectVo();
			guideSelfSelectVo = new GuideSelfSelectVo();
			guideGiftSelectVo = new GuideGiftSelectVo();
			guideViewDic = new Dictionary();
			guideContainer = new Sprite();
			sceneContainer = new Sprite();
			initFuncGuide();*/
			
			guideSettingVo = new GuideSettingVo();
			guideSettingVo.clubId = AppData.getInstance().user.uid;
		}
		private function onEnterFrame(pTime :int):void
		{
			var config :XML = ResourceManager.instance.getResource(ResourceConst.TASKCONFIG);
			if(config == null)return;
			TimeFrameUtil.instance.removeMethod(onEnterFrame);
			initData();
		}
	
		public function get acceptedList():Array
		{
			return _acceptedList;
		}
		
		public function initData():void
		{
			var config 		:XML = ResourceManager.instance.getResource(ResourceConst.GUIDECONFIG);
			if(config == null)
			{
				//TimeFrameUtil.instance.addMethod(onEnterFrame);
				return;
			}
			var guideVo 	:GuideVo;
			var guide 		:Guide;
			var xmlList 	:XMLList;
			for each(var item :XML in config.item)
			{
				guideVo = new GuideVo();
				guideVo.id = item.@id;
				guideVo.parentMod = item.@parentMod;
				guideVo.nextMod = item.@nextMod;
				guideVo.mod = item.@mod;
				guideVo.steps = [];
				for each(var tItem :XML in item.step[0].item)
				{
					guideVo.steps.push(tItem);
				}
				guideDatasDic[guideVo.mod] = guideVo;
				switch(guideVo.mod)
				{
					case 1://欢迎光临
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.WelcomeGuideView");
						break;
					}
					case 2://我的最爱
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.SelectTeamGuideView");
						break;
					}
					/*case 3://一球成名
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.MatchGuideView");
						break;
					}*/
					case 4://新星降临
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.CreateRoleGuideView");
						break;
					}
					case 5://队徽
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.TeamBadgeGuideView");
						break;
					}
					case 6://队服
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.TeamClothGuideView");
						break;
					}
					case 7://巨星加盟
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.StarJoinGuideView");
						break;
					}
					case 8://比赛说明
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.MatchIntroGuideView");
						break;
					}
					case 9://友谊赛
					{
						//guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.FriendMatchGuideView");
						break;
					}
					case 10://添加球友
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.AddFriendGuideView");
						break;
					}
					case 11://出山
					{
						guideViewDic[guideVo.mod] = Reflection.createInstance("com.lz.football.module.guide.view.FinishGuideView");
						break;
					}
				}
			}
			curGuide = guideViewDic[1];
			var pGuide 		:Guide;
			var cGuide		:Guide;
			for(var mod :* in guideViewDic)
			{
				guideVo = guideDatasDic[mod];
				guide = guideViewDic[mod];
				guide.setTask(guideVo.id);
				pGuide = guideViewDic[guideVo.parentMod];
				cGuide = guideViewDic[guideVo.nextMod];
				guide.setParent(pGuide);
				guide.setChild(cGuide);
				guide.steps = guideVo.steps;
			}
			isConfigFinish = true;
		}
		public function getModByTaskId(tid :int):int
		{
			var mod :int;
			var guideVo :GuideVo;
			for(var key :* in guideDatasDic)
			{
				guideVo = guideDatasDic[key];
				if(guideVo.id == tid)
				{
					mod = int(key);
				}
			}
			return mod;
		}
		
		public function initPlayerData():void
		{
			var guidePlayer :GuidePlayerVo;
			var max :int = 70;
			for(var i :int = 0 ;i < 3 ;i ++)
			{
				guidePlayer = new GuidePlayerVo();
				guidePlayer.pname = AppData.getInstance().user.userDetailInfo.nickname;
				guidePlayer.number = i + 1;
				guidePlayer.headurl1 = AppData.getInstance().user.userDetailInfo.headurl;
				guidePlayer.s1 = 30 + Math.floor(Math.random() * max);
				guidePlayer.s2 = 30 + Math.floor(Math.random() * max);
				guidePlayer.s3 = 30 + Math.floor(Math.random() * max);
				guidePlayer.s4 = 30 + Math.floor(Math.random() * max);
				guidePlayer.s5 = 30 + Math.floor(Math.random() * max);
				guidePlayer.s6 = 30 + Math.floor(Math.random() * max);
				guidePlayer.abiliy = 30 + Math.floor(Math.random() * max);
				if(i == 0)
				{
					guidePlayer.place = "DC";
				}
				if(i == 1)
				{
					guidePlayer.place = "DM";
				}
				if(i == 2)
				{
					guidePlayer.place = "AF";
				}
				guidePlayer.initArrowValue();
				selectPlayers.push(guidePlayer);
			}
		}
		/**
		 *服务器属性值列表 
		 * @param list
		 * 
		 */		
		public function playerAttr(list :Array):void
		{
			var guidePlayer :GuidePlayerVo;
			for(var i :int = 0 ;i < list.length ;i ++)
			{
				guidePlayer = new GuidePlayerVo();
				guidePlayer.pname = AppData.getInstance().user.userDetailInfo.nickname;
				guidePlayer.number = i + 1;
				guidePlayer.headurl1 = AppData.getInstance().user.userDetailInfo.headurl;
				guidePlayer.parse(list[i]);
				guidePlayer.initArrowValue();
				selectPlayers.push(guidePlayer);
			}
		}
		/**
		 *初始化功能引导 
		 */		
		public function initFuncGuide():void
		{
			var tempTids :Array = [38,38,38,38,39,40,42,43,44,45,46,47,48,80,49,81,82,64,65,66,67,68,70,71,72];
			/* 38 */guideViewDic[1] = Reflection.createInstance("com.lz.football.module.guide.view_two.WelcomeGuideView");
			/* 38 */guideViewDic[2] = Reflection.createInstance("com.lz.football.module.guide.view_two.ClubGuideView");
			/* 38 */guideViewDic[3] = Reflection.createInstance("com.lz.football.module.guide.view_two.MatchTutorialGuideView");
			/* 38 */guideViewDic[4] = Reflection.createInstance("com.lz.football.module.guide.view_two.TrainGuideView");
			/* 39 */guideViewDic[5] = Reflection.createInstance("com.lz.football.module.guide.view_two.BagGuideView");
			/* 40 */guideViewDic[6] = Reflection.createInstance("com.lz.football.module.guide.view_two.EquipUseGuideView");
			/* 42 */guideViewDic[7] = Reflection.createInstance("com.lz.football.module.guide.view_two.TradeGuideView");
			/* 43 */guideViewDic[8] = Reflection.createInstance("com.lz.football.module.guide.view_two.ExchangeStarGuideView");
			/* 44 */guideViewDic[9] = Reflection.createInstance("com.lz.football.module.guide.view_two.SelfRefreshGuideView");
			/* 45 */guideViewDic[10] = Reflection.createInstance("com.lz.football.module.guide.view_two.FriendGuideView");
			/* 46 */guideViewDic[11] = Reflection.createInstance("com.lz.football.module.guide.view_two.TaskGuideView");
			/* 47 */guideViewDic[12] = Reflection.createInstance("com.lz.football.module.guide.view_two.FirePlayerGuideView");
			/* 48 */guideViewDic[13] = Reflection.createInstance("com.lz.football.module.guide.view_two.ChangeNumGuideView");
			/* 80 */guideViewDic[14] = Reflection.createInstance("com.lz.football.module.guide.view_two.ShopGuideView");
			/* 49 */guideViewDic[15] = Reflection.createInstance("com.lz.football.module.guide.view_two.MatchGuideView");
			/* 81 */guideViewDic[16] = Reflection.createInstance("com.lz.football.module.guide.view_two.BagCourtGuideView");
			/* 82 */guideViewDic[17] = Reflection.createInstance("com.lz.football.module.guide.view_two.ClubUseCourtGuideView");
			/* 63 guideViewDic[17] = Reflection.createInstance("com.lz.football.module.guide.view_two.OneFriendshipGuideView");*/
			/* 64 */guideViewDic[18] = Reflection.createInstance("com.lz.football.module.guide.view_two.TenGradeGuideView");
			/* 65 */guideViewDic[19] = Reflection.createInstance("com.lz.football.module.guide.view_two.SignInGuideView");
			/* 66 */guideViewDic[20] = Reflection.createInstance("com.lz.football.module.guide.view_two.LookTutorialGuideView");
			/* 67 */guideViewDic[21] = Reflection.createInstance("com.lz.football.module.guide.view_two.EuroSearchGuideView");
			/* 68 */guideViewDic[22] = Reflection.createInstance("com.lz.football.module.guide.view_two.OneSelfRefreshGuideView");
			/* 69 guideViewDic[23] = Reflection.createInstance("com.lz.football.module.guide.view_two.OneTourMatchGuideView");*/
			/* 70 */guideViewDic[23] = Reflection.createInstance("com.lz.football.module.guide.view_two.EquipCombineGuideView");
			/* 71 */guideViewDic[24] = Reflection.createInstance("com.lz.football.module.guide.view_two.SaleOnePlayerGuideView");
			/* 72 */guideViewDic[25] = Reflection.createInstance("com.lz.football.module.guide.view_two.FifteenGuideView");
			for(var i :int = 1;i <= 25 ;i++)
			{
				guideViewDic[i].setParent(guideViewDic[(i-1)]);
				guideViewDic[i].setChild(guideViewDic[(i+1)]);
				guideViewDic[i].setTask(tempTids[i-1]);
				guideViewDic[i].guideContainer = guideContainer;
				guideViewDic[i].sceneContainer = sceneContainer;
			}
			curGuide = guideViewDic[1];
		}
		/**
		 *根据任务id得到引导视图 
		 * @param tid
		 * @return 
		 * 
		 */		
		public function getGuideViewByTaskId(tid :int):Guide
		{
			for(var mid :* in guideViewDic)
			{
				if(guideViewDic[mid]&&guideViewDic[mid].taskObj == tid)
				{
					return guideViewDic[mid];
				}
			}
			return null;
		}
		/**
		 *重置该步前视图 
		 * @param tid
		 */		
		public function resetGuideViewUI(tid :int):void
		{
			var curGuideView :Guide = getGuideViewByTaskId(tid);
			if(curGuideView == null)
			{
				return;
			}
			var preGuideView :Guide = curGuideView.parentGuide as Guide;
			while(preGuideView)
			{
				preGuideView.resetUI();
				preGuideView = preGuideView.parentGuide as Guide;
			}
		}
		/**
		 *销毁界面 
		 * 
		 */		
		public function destroyView():void
		{
			var curGuideView :Guide;
			for(var mid :* in guideViewDic)
			{
				curGuideView = guideViewDic[mid];
				curGuideView.clear();
				delete guideViewDic[mid];
			}
			if(guideContainer && guideContainer.parent)
			{
				guideContainer.parent.removeChild(guideContainer);
			}
			if(sceneContainer && sceneContainer.parent)
			{
				sceneContainer.parent.removeChild(sceneContainer);
			}
		}
		/**
		 * destroy();
		 */		
		private function destroy():void
		{
			for each(var tInfo :TaskInfoVo in _acceptedList)
			{
//				tInfo.destroy();
				tInfo = null;
			}
		}
	}
}